import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart' as dio;
import 'package:du_application/http/HttpUtil.dart';
import 'package:du_application/http/Options.dart';
import 'package:du_application/model/UploadModel.dart';
import 'package:du_application/provide/AuthProvider.dart';
import 'package:provide/provide.dart';
import 'package:shared_preferences/shared_preferences.dart';

Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
 upload(context,path) async {
   return await _prefs.then((ins) async{
    Options options =  Options(Provide.value<AuthProvider>(context).token);
     dio.FormData formData = dio.FormData.from({
      "file":dio.UploadFileInfo(path,generateMd5(DateTime.now().toString())+".jpg"),
    });
  return await HttpUtil.getInstance().post("/storage/upload",data:formData,options:options.options).then((data) {
         return UploadModel.fromJson(json.decode(data));
     });
});
  }
String generateMd5(String data) {
  var content = new Utf8Encoder().convert(data);
  var digest = md5.convert(content);
  // 这里其实就是 digest.toString()
  return hex.encode(digest.bytes);
}